//
//  SetViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/15.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "SetViewController.h"

@interface SetViewController () <UIGestureRecognizerDelegate>

@property (nonatomic, retain) UIButton *voice;

@property (nonatomic, retain) UIButton *shake;

@property (nonatomic, retain) UIButton *cache;

@property (nonatomic, retain) UIButton *advice;

@property (nonatomic, retain) UIButton *us;

@property (nonatomic, retain) UIButton *quit;

@property (nonatomic, assign) NSInteger sizefile;

@property (nonatomic, retain) UILabel *getRidOfL;

@end

@implementation SetViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    
    //清除缓存
    
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSLog(@"%@",cachePath);
    NSFileManager *manager = [NSFileManager defaultManager];
    self.sizefile = [[manager attributesOfItemAtPath:cachePath error:nil] fileSize];
    if (self.sizefile > 1024) {
        double a = self.sizefile / 1024;
        double b = self.sizefile % 1024 / 1024.0;
        NSString *str = [NSString stringWithFormat:@"%.1fMB",a + b];
        NSMutableAttributedString *addstr = [[NSMutableAttributedString alloc] initWithString:str];
        [addstr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, str.length - 2)];
        [self.getRidOfL setAttributedText:addstr];
         self.getRidOfL.textColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    } else {
        NSString * str =[NSString stringWithFormat:@"%ldKB",(long)self.sizefile];
        NSMutableAttributedString *addstr = [[NSMutableAttributedString alloc] initWithString:str];
        [addstr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, str.length - 2)];
        [self.getRidOfL setAttributedText:addstr];
         self.getRidOfL.textColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];
    }

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
    self.navigationItem.title = @"设置";
    //状态栏
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0  blue:140 / 255.0  alpha:1.0];
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"but_an.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    
    //图标
    
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 25 * ScreenWidth, 30 * ScreenHeigth, 50 * ScreenWidth, 50 * ScreenHeigth)];
    logo.layer.cornerRadius = 5 * ScreenWidth;
    logo.layer.masksToBounds = YES;
    logo.image = [UIImage imageNamed:@"KJ5D`6Z@`]YI{_57TL1907B.png"];
    [self.view addSubview:logo];
    
    //版本
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(logo.frame.origin.x, logo.frame.origin.y + logo.frame.size.height + 5 * ScreenHeigth, logo.frame.size.width, 20 * ScreenHeigth)];
    la.text = @"版本1.0v";
    la.font = [UIFont systemFontOfSize:10.0 * ScreenWidth];
    la.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    la.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:la];
    
    
    //消息提示音
    
    self.voice = [UIButton buttonWithType:UIButtonTypeCustom];
    self.voice.frame = CGRectMake(0, la.frame.origin.y + la.frame.size.height + 10 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.voice.titleLabel.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.voice setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.voice.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.voice setTitle:@"消息提示音" forState:UIControlStateNormal];
    //设置字体靠左
    self.voice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置字体位置
    self.voice.contentEdgeInsets = UIEdgeInsetsMake(0,15 * ScreenWidth, 0, 0);
    
    [self.voice addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.voice];
    
    UIImageView *imageW = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 * ScreenWidth, self.voice.frame.origin.y + 17 * ScreenHeigth, 8 * ScreenWidth, 12 * ScreenHeigth)];
    imageW.image = [UIImage imageNamed:@"形状-1-拷贝@2x.png"];
    [self.view addSubview:imageW];

    
    //消息震动
    
    self.advice = [UIButton buttonWithType:UIButtonTypeCustom];
    self.advice.frame = CGRectMake(0, self.voice.frame.origin.y + self.voice.frame.size.height + 1 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.advice.titleLabel.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.advice setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.advice.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.advice setTitle:@"消息震动" forState:UIControlStateNormal];
    //设置字体靠左
    self.advice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置字体位置
    self.advice.contentEdgeInsets = UIEdgeInsetsMake(0,15 * ScreenWidth, 0, 0);
    
    [self.advice addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.advice];
    
    UIImageView *shake = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 * ScreenWidth, self.advice.frame.origin.y + 17 * ScreenHeigth, 8 * ScreenWidth, 12 * ScreenHeigth)];
    shake.image = [UIImage imageNamed:@"形状-1-拷贝@2x.png"];
    [self.view addSubview:shake];

    
    
    //清除缓存
    
    self.cache = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cache.frame = CGRectMake(0, self.advice.frame.origin.y + self.voice.frame.size.height + 10 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.cache.titleLabel.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.cache setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.cache.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.cache setTitle:@"清除缓存" forState:UIControlStateNormal];
    //设置字体靠左
    self.cache.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置字体位置
    self.cache.contentEdgeInsets = UIEdgeInsetsMake(0,15 * ScreenWidth, 0, 0);
    
    [self.cache addTarget:self action:@selector(cacheAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.cache];
    
    UIImageView *cache = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 * ScreenWidth, self.cache.frame.origin.y + 17 * ScreenHeigth, 8 * ScreenWidth, 12 * ScreenHeigth)];
    cache.image = [UIImage imageNamed:@"形状-1-拷贝@2x.png"];
    [self.view addSubview:cache];
    
    
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSFileManager *manager = [NSFileManager defaultManager];
    self.sizefile = [[manager attributesOfItemAtPath:cachePath error:nil] fileSize];
    self.getRidOfL = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100 * ScreenWidth, self.cache.frame.origin.y + 10 * ScreenHeigth,100 * ScreenWidth, 27 * ScreenHeigth)];
     self.getRidOfL.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    self.getRidOfL.font = [UIFont systemFontOfSize:13 * ScreenWidth];
    if (self.sizefile > 1024) {
        double a = self.sizefile / 1024;
        double b = self.sizefile % 1024 / 1024.0;
        self.getRidOfL.text = [NSString stringWithFormat:@"%.1fMB",a + b];
    } else {
        self.getRidOfL.text = [NSString stringWithFormat:@"%ldKB",(long)self.sizefile];
    }
    //    self.getRidOfL.text = @"清除缓存";
    self.getRidOfL.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.getRidOfL];

    
    
    
    //消息震动
    
    self.advice = [UIButton buttonWithType:UIButtonTypeCustom];
    self.advice.frame = CGRectMake(0, self.cache.frame.origin.y + self.cache.frame.size.height + 1 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.advice.titleLabel.font = [UIFont systemFontOfSize:15.0 *ScreenWidth];
    [self.advice setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.advice.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.advice setTitle:@"意见反馈" forState:UIControlStateNormal];
    //设置字体靠左
    self.advice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置字体位置
    self.advice.contentEdgeInsets = UIEdgeInsetsMake(0,15 * ScreenWidth, 0, 0);
    
    [self.advice addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.advice];
    
    UIImageView *advice = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 * ScreenWidth, self.advice.frame.origin.y + 17 * ScreenHeigth, 8 * ScreenWidth, 12 * ScreenHeigth)];
    advice.image = [UIImage imageNamed:@"形状-1-拷贝@2x.png"];
    [self.view addSubview:advice];
    
    
    
    //消息震动
    
    self.us = [UIButton buttonWithType:UIButtonTypeCustom];
    self.us.frame = CGRectMake(0, self.advice.frame.origin.y + self.advice.frame.size.height + 1 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.us.titleLabel.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.us setTitleColor:[UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.us.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    [self.us setTitle:@"关于我们" forState:UIControlStateNormal];
    //设置字体靠左
    self.us.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //设置字体位置
    self.us.contentEdgeInsets = UIEdgeInsetsMake(0,15 * ScreenWidth, 0, 0);
    
    [self.us addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.us];
    
    UIImageView *us = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 20 * ScreenWidth, self.us.frame.origin.y + 17 * ScreenHeigth, 8 * ScreenWidth, 12 * ScreenHeigth)];
    us.image = [UIImage imageNamed:@"形状-1-拷贝@2x.png"];
    [self.view addSubview:us];
    
    //退出登录
    
    self.quit = [UIButton buttonWithType:UIButtonTypeCustom];
    self.quit.frame = CGRectMake(0, self.us.frame.origin.y + self.us.frame.size.height + 10 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width, 50 * ScreenHeigth);
    //设置字体大小
    self.quit.titleLabel.font = [UIFont systemFontOfSize:15.0 * ScreenWidth];
    [self.quit setTitleColor:[UIColor colorWithRed:200 / 255.0 green:22 / 255.0 blue:36 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.quit.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
 
    [self.quit setTitle:@"退出当前账号" forState:UIControlStateNormal];
    //设置字体靠左
    self.quit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    [self.quit addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.quit];
    

    
    //添加全屏右滑动手势
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{    
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)orderAction:(UIButton *)sender
{
    
}

- (void)cacheAction:(UIButton *)sender
{
    
    // 1. 获取cache文件夹路径
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSFileManager *fileMana = [NSFileManager defaultManager];
    [fileMana removeItemAtPath:cachePath error:nil];
    self.getRidOfL.text = @"0.0KB";
    self.getRidOfL.textColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1.0];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
